package com.example.lastwork.service;


import com.example.lastwork.common.service.LogicService;
import com.example.lastwork.dao.LineItemDao;
import com.example.lastwork.dao.OrdersDao;
import com.example.lastwork.entity.LineItem;
import com.example.lastwork.entity.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class OrdersService extends LogicService<OrdersDao, Orders, Long> {
    @Resource
    private LineItemDao lineItemDao;

    public OrdersService(@Autowired OrdersDao dao){
        super(dao);
    }

    public LineItem addLineItem(Long ordersId, String lineItemName){
        Orders order = getDAO().getReferenceById(ordersId);
        LineItem lineItem = order.addLineItem(lineItemName);
        lineItemDao.save(lineItem);
        return lineItem;
    }

    public LineItem minusLineItem(Long ordersId, String lineItemName){
        Orders order = getDAO().getReferenceById(ordersId);
        LineItem lineItem = order.minusLineItem(lineItemName);
        if(lineItem == null){
            return null;
        }
        lineItemDao.save(lineItem);
        return lineItem;
    }

    @Transactional
    public int updateTotalPrice(Long ordersId, Double total){
        return getDAO().updateTotalPrice(ordersId, total);
    }
}
